package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandImageIdEnum;
import com.yanqu.road.entity.activity.newyear2023.FireSpFireValueArgs;
import com.yanqu.road.entity.bag.NeedLogSilverData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.entity.log.LogUserSilverChange;
import com.yanqu.road.entity.log.LogVipExp;
import com.yanqu.road.entity.task.args.ConsumeItemArgs;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.activity.businessdepart.BusinessDepartModule;
import com.yanqu.road.server.gameplayer.module.activity.carptrip.CarpTripModule;
import com.yanqu.road.server.gameplayer.module.activity.chess.ChineseChessModule;
import com.yanqu.road.server.gameplayer.module.activity.climbtower.ClimbTowerModule;
import com.yanqu.road.server.gameplayer.module.activity.doublespring.DoubleSpringActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.feast.ShanHeFeastModule;
import com.yanqu.road.server.gameplayer.module.activity.fivehero.FiveHeroModule;
import com.yanqu.road.server.gameplayer.module.activity.crosspasture.CrossPastureModule;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.gameplayer.module.activity.horserun.HorseRunActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.hsisland.HsIslandActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.jieshi.JieShiModule;
import com.yanqu.road.server.gameplayer.module.activity.newyearbag.NewYearBagModule;
import com.yanqu.road.server.gameplayer.module.activity.newyear2023.FireWorkModule;
import com.yanqu.road.server.gameplayer.module.activity.pasture.PastureModule;
import com.yanqu.road.server.gameplayer.module.activity.peakseacraft.PeakSeacraftModule;
import com.yanqu.road.server.gameplayer.module.activity.ruishou.RuiShouModule;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.activity.toyhouse.ToyHouseModule;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.gameplayer.module.activity.xiuxian.XiuXianModule;
import com.yanqu.road.server.gameplayer.module.activity.zhanling.ZhanLingActivityModule;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.drugstore.DrugStoreModule;
import com.yanqu.road.server.gameplayer.module.hunt.HuntModule;
import com.yanqu.road.server.gameplayer.module.manor.ManorFishPondModel;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.server.gameplayer.module.passRoad.PassTransportModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleBoneModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.gameplayer.module.xiaomian.XiaoMianModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.server.manger.activity.able.ActivityAbleMgr;
import com.yanqu.road.server.manger.activity.carptrip.CarpTripMgr;
import com.yanqu.road.server.manger.activity.chinesechess.ChineseChessMgr;
import com.yanqu.road.server.manger.activity.climbtower.ClimbTowerMgr;
import com.yanqu.road.server.manger.activity.crosspasture.CrossPastureMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.doublespring.DoubleSpringActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.activity.newyearbag.NewYearBagMgr;
import com.yanqu.road.server.manger.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.pasture.PastureMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.ruishou.RuiShouMgr;
import com.yanqu.road.server.manger.activity.shrek.DouLuoShrekMgr;
import com.yanqu.road.server.manger.activity.shushan.ShuShanMgr;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.activity.toyhouse.ToyHouseMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.activity.wealthscroll.WealthScrollActivityMgr;
import com.yanqu.road.server.manger.activity.zhanling.ZhanLingActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.manor.FishPondMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.server.push.PushMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 货币模块
 */
public class CurrencyModule extends GeneralModule {

    private BigInteger onlineCostSilver;

    private Map<Integer, NeedLogSilverData> needLogSilverDataMap = new ConcurrentHashMap<>();

    public CurrencyModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        onlineCostSilver = BigInteger.ZERO;
        PlayerModule playerModule = player.getModule(PlayerModule.class);
        if (null == playerModule) {
            log.error("PlayerModule 要在 CurrencyModule 之前。");
            return false;
        }
        if (needLogSilverDataMap == null){
            needLogSilverDataMap = new ConcurrentHashMap<>();
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        //玩家银两变动打个日志
        if (ChannelConfig.USER_SLIVER_LOG_SWITCH == 0){
            return false;
        }
        synchronized (this){
            if (needLogSilverDataMap == null){
                return true;
            }

            for (NeedLogSilverData needLogSilverData : needLogSilverDataMap.values()) {
                //大于0再记录一下
                if (needLogSilverData.getCount().compareTo(BigInteger.ZERO) > 0){
                    //最后更新时间
                    Date date = new Date(needLogSilverData.getLastUpdateTime());
                    //new一个log
                    AutoLogMgr.add(new LogUserSilverChange(getUserId(), needLogSilverData.getSonType(), needLogSilverData.getCount().toString(), date));
                    //清空
                    needLogSilverData.setCount(BigInteger.ZERO);
                }
            }
        }
        return true;
    }

    /**
     * 扣除元宝
     */
    private long removeIngots(long value, eLogMoneyType master, eLogMoneyType son, String desc) {
        synchronized (this) {
            long ingots = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            if (value > 0 && value <= ingots) {
                AttributeModule atModule = player.getModule(AttributeModule.class);
                atModule.onDataChange(ePlayerAttrType.Ingots);
                atModule.addAttribute(ePlayerAttrType.Ingots, value * -1);
                atModule.addAttribute(ePlayerAttrType.TotalConsumeIngots, value);
                LogMgr.addLogIngots(player.getUserId(), value, false, master, son, ingots,
                        atModule.getAttribute(ePlayerAttrType.Ingots), desc, player.getUserInfo().getVipExp());
                player.getModule(XiaoMianModule.class).addIngotsConsume(value);
                return value;
            } else {
                return 0;
            }
        }
    }

    /**
     * 扣除元宝(可以到负数)
     */
    private long removeIngotsToNegative(long value, eLogMoneyType master, eLogMoneyType son, String desc) {
        synchronized (this) {
            long ingots = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            if (value > 0) {
                AttributeModule atModule = player.getModule(AttributeModule.class);
                atModule.onDataChange(ePlayerAttrType.Ingots);
                atModule.addAttribute(ePlayerAttrType.Ingots, value * -1);

                LogMgr.addLogIngots(player.getUserId(), value, false, master, son, ingots,
                        atModule.getAttribute(ePlayerAttrType.Ingots), desc, player.getUserInfo().getVipExp());
                return value;
            } else {
                return 0;
            }
        }
    }

    private long addIngots(long value, eLogMoneyType master, eLogMoneyType son) {
        synchronized (this) {
            long ingots = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            if (value > 0) {
                AttributeModule atModule = player.getModule(AttributeModule.class);
                atModule.onDataChange(ePlayerAttrType.Ingots);
                atModule.addAttribute(ePlayerAttrType.Ingots, value);
                atModule.addAttribute(ePlayerAttrType.TotalIngots, value);
                LogMgr.addLogIngots(player.getUserId(), value, true, master, son, ingots,
                        atModule.getAttribute(ePlayerAttrType.Ingots), "", player.getUserInfo().getVipExp());

                //元宝变化埋点
//                ResourceChangeLog.trackResourceChange(player, GameConfig.GAME_MONEY_INGOTS, 1, value, ingots, master.getValue());
                //ResourceChangeLog.trackResourceGetSingle(player, GameConfig.GAME_MONEY_INGOTS, "", BigInteger.valueOf(value), son, ingots, atModule.getAttribute(ePlayerAttrType.Ingots));
            return value;
            } else {
                return 0;
            }
        }
    }

    //后台扣除增加玩家元宝
    public long changeIngotsBackend(long value,eLogMoneyType master, eLogMoneyType son){
        synchronized (this) {
            long ingots = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            AttributeModule atModule = player.getModule(AttributeModule.class);
            atModule.onDataChange(ePlayerAttrType.Ingots);
            atModule.addAttribute(ePlayerAttrType.Ingots, value);
            if(value > 0){
                atModule.addAttribute(ePlayerAttrType.TotalIngots, value);
            }else {
                atModule.addAttribute(ePlayerAttrType.TotalConsumeIngots, value * -1);
            }
            LogMgr.addLogIngots(player.getUserId(), value, true, master, son, ingots,
                    atModule.getAttribute(ePlayerAttrType.Ingots), "", player.getUserInfo().getVipExp());
            return value;
        }
    }

    /**
     * 扣除银两(可以扣除到负数)
     */
    private BigInteger removeSilverToNegative(BigInteger value) {
        synchronized (this) {
            BigInteger silver = player.getModule(PlayerModule.class).getUserInfo().getSilver();
            //只需要扣除的为正数
            if (value.compareTo(BigInteger.ZERO) > 0) {
                AttributeModule atModule = player.getModule(AttributeModule.class);
                atModule.onDataChange(ePlayerAttrType.Silver);
                player.getModule(PlayerModule.class).getUserInfo().setSilver(silver.subtract(value));
                onlineCostSilver = onlineCostSilver.add(value);
                return value;
            } else {
                return BigInteger.ZERO;
            }
        }
    }

  // 扣除银两
    private BigInteger removeSilver(BigInteger value, eLogMoneyType son) {
        synchronized (this) {
            BigInteger silver = player.getModule(PlayerModule.class).getUserInfo().getSilver();
            if (value.compareTo(BigInteger.ZERO) > 0 && value.compareTo(silver) <= 0) {
                AttributeModule atModule = player.getModule(AttributeModule.class);
                atModule.onDataChange(ePlayerAttrType.Silver);
                player.getModule(PlayerModule.class).getUserInfo().setSilver(silver.subtract(value));
                onlineCostSilver = onlineCostSilver.add(value);
                if (ChannelConfig.USER_SLIVER_LOG_SWITCH == 1){
                    //拿出日志
                    NeedLogSilverData silverLog = getOrCreateSilverLog(son.getValue());
                    if (silverLog != null){
                        silverLog.setCount(silverLog.getCount().add(value));
                        silverLog.setLastUpdateTime(System.currentTimeMillis());
                    }
                }
                return value;
            } else {
                return BigInteger.ZERO;
            }
        }
    }

    private NeedLogSilverData getOrCreateSilverLog(int sonType){
        if (sonType <= 0){
            log.info("remove sliver log error, type <= 0");
            return null;
        }
        if (needLogSilverDataMap == null){
            needLogSilverDataMap = new ConcurrentHashMap<>();
        }
        if (needLogSilverDataMap.containsKey(sonType)){
            return needLogSilverDataMap.get(sonType);
        }else {
            NeedLogSilverData needLogSilverData = new NeedLogSilverData(sonType,BigInteger.ZERO,System.currentTimeMillis());
            needLogSilverDataMap.put(needLogSilverData.getSonType(), needLogSilverData);
            return needLogSilverData;
        }
    }

    private BigInteger removePatronsExp(BigInteger value, eLogMoneyType son) {
        BigInteger patronsExp = player.getModule(PlayerModule.class).getUserInfo().getPatronsExp();
        if (value.compareTo(BigInteger.ZERO) > 0 && value.compareTo(patronsExp) <= 0) {
            AttributeModule atModule = player.getModule(AttributeModule.class);
            atModule.onDataChange(ePlayerAttrType.PatronsExp);
            player.getModule(PlayerModule.class).getUserInfo().setPatronsExp(patronsExp.subtract(value));
            return value;
        } else {
            return BigInteger.ZERO;
        }
    }

    private BigInteger removePatronsExpToNegative(BigInteger value, eLogMoneyType son) {
        BigInteger patronsExp = player.getModule(PlayerModule.class).getUserInfo().getPatronsExp();
        if (value.compareTo(BigInteger.ZERO) > 0) {
            AttributeModule atModule = player.getModule(AttributeModule.class);
            atModule.onDataChange(ePlayerAttrType.PatronsExp);
            player.getModule(PlayerModule.class).getUserInfo().setPatronsExp(patronsExp.subtract(value));
            return value;
        } else {
            return BigInteger.ZERO;
        }
    }

    private BigInteger removePrestigeToNegative(BigInteger value, eLogMoneyType son) {
        BigInteger prestige = player.getModule(PlayerModule.class).getUserInfo().getPrestige();
        if (value.compareTo(BigInteger.ZERO) > 0) {
            AttributeModule atModule = player.getModule(AttributeModule.class);
            atModule.onDataChange(ePlayerAttrType.Prestige);
            player.getModule(PlayerModule.class).getUserInfo().setPrestige(prestige.subtract(value));
            //招商更新玩家信息
            ZsMgr.addNeedUploadUserInfo(player);
            return value;
        } else {
            return BigInteger.ZERO;
        }
    }

    private BigInteger removePrestige(BigInteger value, eLogMoneyType son) {
        BigInteger prestige = player.getModule(PlayerModule.class).getUserInfo().getPrestige();
        if (value.compareTo(BigInteger.ZERO) > 0 && value.compareTo(prestige) <= 0) {
            AttributeModule atModule = player.getModule(AttributeModule.class);
            atModule.onDataChange(ePlayerAttrType.Prestige);
            player.getModule(PlayerModule.class).getUserInfo().setPrestige(prestige.subtract(value));
            //招商更新玩家信息
            ZsMgr.addNeedUploadUserInfo(player);
            return value;
        } else {
            return BigInteger.ZERO;
        }
    }

    //增加银两
    private void addSilver(BigInteger value, eLogMoneyType son) {
        synchronized (this) {
            BigInteger silver = player.getModule(PlayerModule.class).getUserInfo().getSilver();
            if (value.compareTo(BigInteger.ZERO) > 0) {
                player.getModule(PlayerModule.class).getUserInfo().setSilver(silver.add(value));
                player.getModule(AttributeModule.class).onDataChange(ePlayerAttrType.Silver);
            }
        }
    }

    //增加vip经验
    private void addVipExp(BigInteger value, eLogMoneyType son) {
        synchronized (this) {
            int vipExp = value.intValue();
            long originalValue = player.getUserInfo().getVipExp();
            player.getModule(RechargeModule.class).addVipExp(vipExp);
            AutoLogMgr.add(new LogVipExp(player.getUserId(), originalValue, vipExp, originalValue + vipExp, 3, "游戏内奖励")); //游戏内奖励
        }
    }

    //增加vip经验
    private void addVipExpNoAddActivityProgress(BigInteger value, eLogMoneyType son) {
        synchronized (this) {
            int vipExp = value.intValue();
            long originalValue = player.getUserInfo().getVipExp();
            player.getModule(PlayerModule.class).addVipExp(vipExp);
            AutoLogMgr.add(new LogVipExp(player.getUserId(), originalValue, vipExp, originalValue + vipExp, 4, "公众号奖励"));//公众号奖励
        }
    }

    private void addPatronsExp(BigInteger value, eLogMoneyType son) {
        BigInteger exp = player.getModule(PlayerModule.class).getUserInfo().getPatronsExp();
        if (value.compareTo(BigInteger.ZERO) > 0) {
            player.getModule(PlayerModule.class).getUserInfo().setPatronsExp(exp.add(value));
            player.getModule(AttributeModule.class).onDataChange(ePlayerAttrType.PatronsExp);
        }
    }

    private void addPrestige(BigInteger value, eLogMoneyType son) {
        BigInteger prestige = player.getModule(PlayerModule.class).getUserInfo().getPrestige();
        if (value.compareTo(BigInteger.ZERO) > 0) {
            player.getModule(PlayerModule.class).getUserInfo().setPrestige(prestige.add(value));
            player.getModule(AttributeModule.class).onDataChange(ePlayerAttrType.Prestige);
            //招商更新玩家信息
            ZsMgr.addNeedUploadUserInfo(player);
            // 同步榷场玩家基本信息
            MonopolyMarketMgr.addNeedUploadUserInfo(player);
            NewMonopolyMarketMgr.addNeedUploadUserInfo(player);
            LogMgr.addLogGoods(GameConfig.GAME_MONEY_PRESTIGE, player.getUserId(), true, value.longValue(), prestige.add(value).longValue(), son, String.valueOf(player.getTitleId()), player.getUserInfo().getVipExp());
        }
    }

    public Property addCurrency(String value, eLogMoneyType master, eLogMoneyType son) {
        Property reward = PropertyHelper.parseStringToProperty(value);
        addCurrency(reward, master, son);
        return reward;
    }

    public void addCurrency(Property value, eLogMoneyType master, eLogMoneyType son) {
        addCurrency(value, master, son, new Date());
    }

    /**
     * @param value  要添加到背包的物品集合，把所有能放到背包的都放进去，
     * @param master
     * @param son
     * @return 如果有物品满了放不进背包，则返回大于0 返回值根据与或的规则
     */
    public void addCurrency(Property value, eLogMoneyType master, eLogMoneyType son, Date date) {
        if (value != null) {
            List<Integer> goodsIdList = new ArrayList<>(value.getGoods().keySet());
            BagModule bagModule = player.getModule(BagModule.class);
            for (int goodsId : goodsIdList) {
                //记录活动积分
                addActivityScore(goodsId, value.getCountByGoodsId(goodsId).longValue());
                //如果是活动基金进度道具,累计活动基金进度
                addActivityFundProgress(goodsId, value.getCountByGoodsId(goodsId).longValue());

                if (GameConfig.GAME_MONEY_INGOTS == goodsId) {
                    long ingots = value.getCountByGoodsId(goodsId).longValue();
                    addIngots(ingots, master, son);
                    if(ingots>0){
                        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.OUTPUT,eItemChangeEnums.TypeItem.RESOURCE,son,goodsId, BigInteger.valueOf(ingots));
                    }
                } else if (GameConfig.GAME_MONEY_VIPEXP == goodsId) {
                    addVipExp(value.getCountByGoodsId(goodsId), son);
                } else if(GameConfig.GAME_MONEY_VIPEXP_NO_ADD_ACTIVITY == goodsId) {
                    addVipExpNoAddActivityProgress(value.getCountByGoodsId(goodsId), son);
                } else if (GameConfig.GAME_MONEY_SILVER == goodsId) {
                    addSilver(value.getCountByGoodsId(goodsId), son);
                } else if (GameConfig.GAME_MONEY_PATRONS_EXP == goodsId) {
                    BigInteger patronsExp=value.getCountByGoodsId(goodsId);
                    addPatronsExp(value.getCountByGoodsId(goodsId), son);
                    PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.OUTPUT,eItemChangeEnums.TypeItem.RESOURCE,son,goodsId, patronsExp);
                }  else if (GameConfig.GAME_MONEY_PRESTIGE == goodsId) {
                    addPrestige(value.getCountByGoodsId(goodsId), son);
                } else if (GoodsMgr.isPatronsGoods(goodsId)) {//门客系统
                    player.getModule(PatronsModule.class).addPatrons(goodsId, master, son);
                } else if (GoodsMgr.isBeautyGoods(goodsId)) {//美女系统
                    player.getModule(BeautyModule.class).addBeauty(goodsId, master, son);
                } else if (GoodsMgr.isVehicleGoods(goodsId)) {//珍兽系统
                    int num = value.getCountByGoodsId(goodsId).intValue();
                    player.getModule(VehicleModule.class).addVehicle(goodsId, num, master, son);
                } else if (GoodsMgr.isDecorationGoods(goodsId)) {//饰品系统
                    player.getModule(DecorationModule.class).addDecoration(goodsId, value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GameConfig.GAME_ITEM_UNION_EXP == goodsId) {//商会经验值
                    player.getModule(CrossUnionModule.class).addUnionExp(value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GameConfig.GAME_ITEM_UNION_WEALTH == goodsId) {//商会财富值
                    player.getModule(CrossUnionModule.class).addUnionWealth(value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GameConfig.GAME_ITEM_UNION_CONTRIBUTION == goodsId) {//商会个人贡献
                    player.getModule(CrossUnionModule.class).addContribution(value.getCountByGoodsId(goodsId).longValue(), master, son);
                }else if (GameConfig.BANQUET_INTEGRAL == goodsId) {//宴会积分
                    player.getModule(BanquetModule.class).addIntegral(value.getCountByGoodsId(goodsId).longValue(), master, son);
                }else if (GameConfig.BANQUET_POPULARITY == goodsId) {//宴会人气
                    player.getModule(BanquetModule.class).addPopularity(value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GameConfig.BANQUET_CROSS_INVITATION_1 == goodsId || GameConfig.BANQUET_CROSS_INVITATION_2 == goodsId || GameConfig.BANQUET_CROSS_INVITATION_3 == goodsId) {//跨服宴会1道具
                    //跨服宴會的道具會特殊處理變成帖子，不會進入背包
                }else if (GoodsMgr.isPassTransportGoods(goodsId)) {//关卡座驾
                    player.getModule(PassTransportModule.class).addTransport(goodsId, master, son);
                }else if (GameConfig.SECRET_VOLUME_EXP_ID == goodsId) {//密卷活動升級經驗
                    player.getModule(SecretVolumeModule.class).addSecretVolumeExp(value.getCountByGoodsId(goodsId).longValue());
                }else if (GameConfig.SECRET_VOLUME_INTEGRAL_ID == goodsId) {//密卷活動升級积分
                    player.getModule(SecretVolumeModule.class).addSecretVolumeIntegral(value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (goodsId == StrongholdActivityMgr.getWildIntegarlId()) {
                    player.getModule(StrongholdModule.class).addRewardPoints(value.getCountByGoodsId(goodsId).intValue());
                } else if (GoodsMgr.isUnionFlag(goodsId) || GoodsMgr.isUnionFlagParts(goodsId)) {
                    //CrossUnionMgr.addUnionFlag( player.getUserInfo().getUnionUid(),goodsId);
                    CrossUnionMgr.addReward(player.getUserInfo(), goodsId, 1, son.getValue(), 1);
                } else if (DaFuWengMgr.isDollItem(goodsId)) { // 大富翁人偶道具
                    DaFuWengMgr.addDoll(player, goodsId, value.getCountByGoodsId(goodsId));
                } else if (ZhanLingActivityMgr.isZhanLingScore(goodsId)) {
                    player.getModule(ZhanLingActivityModule.class).addScore(value.getCountByGoodsId(goodsId).intValue(), goodsId);
                } else if (GameConfig.GAME_ITEM_CREDIT == goodsId) {
                    player.getModule(HouseModule.class).addHouseCredit(value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GameConfig.SKIN_VOLUME_PIECES_ID == goodsId) {   //残卷
                    player.getModule(SkinVolumeModule.class).addPieces(value.getCountByGoodsId(goodsId).longValue());
                } else if (FishPondMgr.isFishItem(goodsId)) {   //鱼类
                    ManorFishPondModel model = player.getModule(ManorModule.class).getManorFishPondModel();
                    if (model != null && model.getFishPondData() != null) {
                        model.addFishCurrency(goodsId, value.getCountByGoodsId(goodsId).longValue(), son);
                    }
                } else if (GameConfig.FISHPOND_FISH_EXP_ITEM_ID == goodsId) {   //鱼食
                    ManorFishPondModel model = player.getModule(ManorModule.class).getManorFishPondModel();
                    if (model != null && model.getFishPondData() != null) {
                        model.addExp(goodsId, value.getCountByGoodsId(goodsId).longValue(), son);
                    }
                }  else if (isExpressionPackage(goodsId)) {   //表情包
                    autoUseExpressionPackage(goodsId, master, son);
                }else if (DouLuoShrekMgr.getCollegeScoreItemId() == goodsId) {   //史瑞克学院积分道具id
                    addGoods(bagModule, goodsId, value.getCountByGoodsId(goodsId), master, son);
                } else if (GoodsMgr.isAbleGoods(goodsId)) { // 凭证
                    ActivityAbleMgr.addAbleItem(getUserId(), goodsId, value.getCountByGoodsId(goodsId).longValue(), son);
                } else if (isTimeLimitHead(goodsId)) { // 限时头像
                    player.getModule(DecorationModule.class).addTimeLimitHead(goodsId, value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (ToyHouseMgr.isCandyItem(goodsId)) { // 玩具屋成就道具
                    player.getModule(ToyHouseModule.class).addCandy(goodsId, value.getCountByGoodsId(goodsId).longValue(), son);
                }else if (goodsId == ChineseChessMgr.getChineseChessReviveItemId()) { // 可以复活
                    player.getModule(ChineseChessModule.class).canRevive(goodsId, value.getCountByGoodsId(goodsId).longValue(), master, son);
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (goodsId == PastureMgr.getPastureRestartItemId()) { // 可以复活
                    player.getModule(PastureModule.class).canRevive();
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (goodsId == CrossPastureMgr.getPastureRestartItemId()) { // 可以复活
                    player.getModule(CrossPastureModule.class).canRevive();
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (ShuShanMgr.isPrestige(goodsId)) {
                    ShuShanMgr.addPrestige(player, value.getCountByGoodsId(goodsId).longValue());
                } else if (GoodsMgr.isHonorScore(goodsId)) {
//                    CrossUnionMgr.addReward(player.getUserInfo(), goodsId, value.getCountByGoodsId(goodsId).longValue(), son.getValue(), 1);
//                    log.error("出现了配置荣誉的奖励没有正常过滤领取的{}:{}",player.getUserId(),PropertyHelper.parsePropertyToString(value));
                } else if (GoodsMgr.isFiveHeroAddItem(goodsId)) {
                    //加到背包去当日志
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                    player.getModule(FiveHeroModule.class).addItem(goodsId,num);
                } else if (GameConfig.DRUGSTORE_ITEM_ID_EXP == goodsId) { //药铺经验
                    player.getModule(DrugStoreModule.class).addExperience(goodsId, value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (TongBingMgr.isHorseItem(goodsId)) {
                    TongBingMgr.addHorse(player, goodsId, value.getCountByGoodsId(goodsId).longValue(), son.getValue());
                } else if (ChuHanMgr.isHorseItem(goodsId)) {
                    ChuHanMgr.addHorse(player, goodsId, value.getCountByGoodsId(goodsId).longValue(), son.getValue());
                } else if (TonBingMgr.isHorseItem(goodsId)) {
                    TonBingMgr.addHorse(player, goodsId, value.getCountByGoodsId(goodsId).longValue(), son.getValue());
                } else if (ChannelTonBingMgr.isHorseItem(goodsId)) {
                    ChannelTonBingMgr.addHorse(player, goodsId, value.getCountByGoodsId(goodsId).longValue(), son.getValue());
                }else if (ClimbTowerMgr.isSpecialGoods(goodsId)) { //激斗通天塔
                    player.getModule(ClimbTowerModule.class).addSpecialGoods(goodsId, value.getCountByGoodsId(goodsId));
                } else if (CarpTripMgr.isSpecialGoods(goodsId)) { //鲤鱼跃龙门
                    player.getModule(CarpTripModule.class).addSpecialGoods(goodsId, value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isTombDollItem(goodsId)){
                    player.getModule(TombModule.class).addDoll(goodsId,value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isWasheCard(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    if (num.compareTo(BigInteger.ZERO) > 0) {
                        player.getModule(WasheModule.class).checkGetWasheCard(goodsId, num.longValue());
                        addGoods(bagModule, goodsId, num, master, son);
                    }
                } else if (GoodsMgr.isWasheSkill(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    if (num.compareTo(BigInteger.ZERO) > 0) {
                        player.getModule(WasheModule.class).checkGetWasheSkill(goodsId, num.longValue());
                        addGoods(bagModule, goodsId, num, master, son);
                    }
                } else if (GoodsMgr.isBadgeUnlockItem(goodsId)) {
                    player.getModule(BadgeModule.class).addBadgeByItem(goodsId);
                } else if (WasheMgr.isMultiCard(goodsId)) {
                    int num = value.getCountByGoodsId(goodsId).intValue();
                    player.getModule(WasheModule.class).useBuffItem(goodsId, num);
                } else if (GoodsMgr.isCricketGoods(goodsId)) {//蛐蛐系统
                    player.getModule(CricketModule.class).addCricket(goodsId, value.getCountByGoodsId(goodsId), master, son, true);
                } else if (GameConfig.CRICKET_EXP_ITEM_ID == goodsId) { // 虫师积分
                    player.getModule(CricketModule.class).addCricketScore(value.getCountByGoodsId(goodsId));
                } else if (GameConfig.CRICKET_DRAWPICE_ITEM_ID == goodsId) { // 蛐蛐绘卷碎片道具id
                    player.getModule(CricketModule.class).addCricketDrawPieceItem(value.getCountByGoodsId(goodsId), master, son);
                } else if (GoodsMgr.isCricketCourtBadgeExp(goodsId)) {
                    player.getModule(CricketModule.class).addCricketCourtBadgeExp(value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isCricketCourtTokenExp(goodsId)) {
                    player.getModule(CricketModule.class).addTokenExp(value.getCountByGoodsId(goodsId));
                } else if (FireWorkMgr.getScoreId() == goodsId) { // 积分道具id
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    player.getModule(FireWorkModule.class).addScore(num,son);
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (GoodsMgr.isRealReward(goodsId)) {
                    long num = value.getCountByGoodsId(goodsId).longValue();
                    player.getModule(RealRewardModule.class).addRealReward(goodsId, num);
                } else if (GoodsMgr.isSpecialSkinBox(goodsId)) {
                    long num = value.getCountByGoodsId(goodsId).longValue();
                    player.getModule(SkinModule.class).openSpecialSkinBox(goodsId, num, master, son);
                } else if (NewYearBagMgr.isFuDai(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    player.getModule(NewYearBagModule.class).addHongBaoByFuDai(goodsId, num.intValue(), son.getValue());
                } else if (GoodsMgr.isXiuXianGoods(goodsId)) {
                    player.getModule(XiuXianModule.class).addXiuXianGoods(goodsId, value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isShanHeFeastGoods(goodsId)) {
                    player.getModule(ShanHeFeastModule.class).addFeastGoods(goodsId, value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isXiuXianJiYuanDian(goodsId)) {
                    player.getModule(XiuXianModule.class).addXiuXianJiYuanDian(value.getCountByGoodsId(goodsId));
                }else if (GoodsMgr.isShanHeFeastLingGanDian(goodsId)) {
                    player.getModule(ShanHeFeastModule.class).addFeastLingGanDian(value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isJieShiJiaoZi(goodsId)) {
                    JieShiModule jieShiModule = player.getModule(JieShiModule.class);
                    jieShiModule.addJiaoZi(value.getCountByGoodsId(goodsId).longValue());
                    jieShiModule.syncUserData();
                } else if (PeakSeacraftMgr.isNeedRecycleItem(goodsId)) {
                    player.getModule(PeakSeacraftModule.class).GoodsToMail(goodsId, value.getCountByGoodsId(goodsId).longValue(), master, son);
                } else if (GoodsMgr.isWasheJinBiaoScoreItemId(goodsId)) {
                    player.getModule(WasheModule.class).addJinBiaoScore(value.getCountByGoodsId(goodsId));
                } else if (FiveHeroMgr.isPopularityItemId(goodsId)) {
                    player.getModule(FiveHeroModule.class).addPopularity(goodsId,value.getCountByGoodsId(goodsId));
                } else if (GoodsMgr.isCouponItem(goodsId)) {
                    long num = value.getCountByGoodsId(goodsId).longValue();
                    player.getModule(CouponModule.class).addCouponItem(goodsId, num, master, son);
                } else if (GoodsMgr.isAreaFightMedalGoods(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    EnterNameActivityMgr.handleMedal(player, goodsId, num.longValue());
                } else if (SnowGardenMgr.isSkinItem(goodsId)) { // 冰雪游园皮肤
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    SnowGardenMgr.addSkin(player, goodsId, num.intValue());
                } else if (RuiShouMgr.isRuiShouScoreItemId(goodsId)) { // 祥瑞赐福积分道具
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    player.getModule(RuiShouModule.class).addScore(num.longValue());
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (DoubleSpringActivityMgr.isDoubleSpringZhanLingItem(goodsId)) {
                    player.getModule(DoubleSpringActivityModule.class).addExp(value.getCountByGoodsId(goodsId).longValue());
                } else if (GoodsMgr.isMeiTuanQuan(goodsId)) {       // 美团无门槛券
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                    // 发个邮件告知玩家
                    String mailTitle = MultipleLanguageMgr.getContent(MailManager.MEITUANDINNER_MAIL_TITLE, player.getLanguage());
                    String mailContent = MultipleLanguageMgr.getContent(MailManager.MEITUANDINNER_MAIL_CONTENT, player.getLanguage());
                    MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);
                } else if (SuiTangMgr.isSuiTangItem(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    player.getModule(SuiTangModule.class).addMinXin(num.longValue());
                } else if (GoodsMgr.isVehicleBone(goodsId)) {
                    player.getModule(VehicleBoneModule.class).addBone(goodsId, value.getCountByGoodsId(goodsId));
                    LogGoodsChange(goodsId, true, value.getCountByGoodsId(goodsId), 0, 0, master, son);
                } else if (HsIslandImageIdEnum.IMAGE_ADD_SCORE.getType() == goodsId) {
                    player.getModule(HsIslandActivityModule.class).unlockScoreImageBfScore();
                } else if (GoodsMgr.goodIsThisType(goodsId,eGoodsType.HorseRunSkin.getValue())){
                    player.getModule(HorseRunActivityModule.class).addPaySkin(goodsId);
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    addGoods(bagModule, goodsId, num, master, son);
                } else if (WealthScrollActivityMgr.isNewWealthScrollExp(goodsId)) {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    player.getModule(WealthScrollModule.class).addWealthScrollExp(num.longValue());
                    LogGoodsChange(goodsId, true, num, 0, 0, master, son);
                } else {
                    BigInteger num = value.getCountByGoodsId(goodsId);
                    if (goodsId == eItemId.SHUIHUYEZICARD_INVITATION_ITEM.getValue()) {
                        player.getModule(ShuiHuYeZiCardModule.class).addPropNum(num.longValue(), true);
                    } else if (GoodsMgr.isDaoMuJiBaiShuLiangGoods(goodsId)) {
                        player.getModule(AttributeModule.class).addAttribute(ePlayerAttrType.DaoMuJiShaShuLiang, num.longValue());
                    }
                    addGoods(bagModule, goodsId, num, master, son);
                }
            }
        }
    }

    /**
     * 获取活动类型
     *
     * @param goodsId
     * @return
     */
    public List<Integer> getActivityIds(int goodsId) {
        // 先找配置
        List<Integer> list = findActivityIdFromConfig(goodsId);

        //如果找不到 就按原来的方式查找
        List<Integer> activityIds = new ArrayList<>();
        Map<Integer, List<Integer>> activityTypeMap = findActivityType(goodsId);
        for (Map.Entry<Integer, List<Integer>> entry : activityTypeMap.entrySet()) {
            //万能活动不在这里做处理
            List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(entry.getKey());
            if(entry.getKey() == eActivityType.ChefDuel.getValue()){
                activityInfoList.addAll(RelationActivityMgr.getOpenActivityInfoList(eActivityType.ChefDuel.getValue(),entry.getValue().get(0)));
            }
            if (activityInfoList.size() == 0) {
                //没有加载在normalActivityMgr里的活动类型
                if (entry.getKey() == eActivityType.CrossTradeWarActivity.getValue()) { //跨服商战活动
                    activityInfoList.add(TradeWarActivityMgr.getOpenActivityInfo());
                } else if (entry.getKey() == eActivityType.OreWarActivity.getValue()) { //盐场活动
                    activityInfoList.addAll(OreWarActivityMgr.getOpenActivityInfoList());
                } else if (entry.getKey() == eActivityType.StrongholdWarActivity.getValue()) {  //据点战活动
                    activityInfoList.add(StrongholdActivityMgr.getActivityInfo());
                } else if (entry.getKey() == eActivityType.CrossUnionWarActivity.getValue()) {  //跨服商会战活动
                    activityInfoList.addAll(UnionWarActivityMgr.getOpenActivityInfo());
                }else if (entry.getKey() == eActivityType.CookingBoyActivity.getValue()) {  //跨服在内存活动 活动和活动类型对应找出活动多个活动大类型对应不同积分道具
                    activityInfoList.addAll(RelationActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),entry.getValue().get(0)));
                }
            }
            for (ActivityInfo activityInfo : activityInfoList) {
                if (entry.getValue().size() >= 1 && entry.getValue().contains(activityInfo.getChildType())) {
                    activityIds.add(activityInfo.getActivityId());
                }
            }
        }
        HashSet<Integer> hashSet = new HashSet<>();
        if (list != null) {
            hashSet.addAll(list);
        }
        hashSet.addAll(activityIds);
        return new ArrayList<>(hashSet);
    }

    private List<Integer> findActivityIdFromConfig(int goodsId) {
        List<Integer> list = new ArrayList<>();
        List<ActivityInfo> allActivityInfos = ActivityMgr.getAllActivityInfos();
        if (allActivityInfos != null) {
            for (ActivityInfo activityInfo : allActivityInfos) {
                if (ActivityMgr.activityInShowTime(activityInfo)) {
                    //在活动期间
                    int itemId = ActivityMgr.getMallExchangeScoreItemId(activityInfo.getActivityId());
                    if (itemId == goodsId) {
                        list.add(activityInfo.getActivityId());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 查找活动类型
     * @param goodsId
     * @return
     */
    public Map<Integer, List<Integer>> findActivityType(int goodsId) {
        Map<Integer, List<Integer>> activityTypeMap = new ConcurrentHashMap<>();
        for (eActivityScoreFindType value : eActivityScoreFindType.values()) {
            if (goodsId == value.itemId.getValue()) {
                if (!activityTypeMap.containsKey(value.activityType.getValue())) {
                    activityTypeMap.put(value.activityType.getValue(), new ArrayList<>());
                }
                activityTypeMap.get(value.activityType.getValue()).add(value.littleType);
            }
        }
        return activityTypeMap;
    }

    /**
     * 记录活动积分
     * @param goodsId
     * @param score
     */
    public void addActivityScore(int goodsId, long score) {
        try {
            List<Integer> activityIds = getActivityIds(goodsId);
            for (Integer activityId : activityIds) {
                player.getModule(ActivityMallModule.class).addScore(activityId, goodsId, score);
            }
        } catch (Exception ex) {
            log.error("add activity score error!, goodsId:{}, score:{}", goodsId, score);
        }
    }

    public void addActivityFundProgress(int goodsId, long goodsNum) {
        try {
            List<Integer> activityIds = ActivityMgr.getAllActivityFundActivityIds(goodsId);
            for (Integer activityId : activityIds) {
                player.getModule(ActivityFundModule.class).addProgress(activityId, goodsId, goodsNum);
            }
        } catch (Exception ex) {
            log.error("add activity fund progress error!, goodsId:{}, goodsNum:{}", goodsId, goodsNum);
        }

    }

    public void LogGoodsChange(int goodsId, boolean isGet, BigInteger goodsCount, long oldValue, long nowValue, eLogMoneyType master, eLogMoneyType son){
        if(goodsId == GameConfig.GAME_ITEM_ESTATE_DAILY_WINE){//累计乔迁风云每日陈酿收获
            if(isGet) {
                UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
                if (null != userEstateWarData) {
                    userEstateWarData.setTotalDailyRewardCount(userEstateWarData.getTotalDailyRewardCount().add(goodsCount));
                    //成就、任务
                    player.notifyListener(eGamePlayerEventType.EstateWarTotalDailyRewardCount.getValue(), userEstateWarData);
                }
            }
            LogMgr.addLogEstateWine(player.getUserId(), isGet, goodsCount.longValue(), oldValue, nowValue, master.getValue(), son.getValue());
        }else if(goodsId == GameConfig.GAME_MONEY_HUNT_SCORE){
            if(isGet) {
                player.getModule(HuntModule.class).addHuntScore(goodsCount.intValue());
            }
        }else if(GoodsMgr.isSpFireGood(goodsId)){
            if(isGet) {
                player.notifyListener(eGamePlayerEventType.FireWorkSpGetCount.getValue(), new FireSpFireValueArgs(goodsCount,goodsId));
            }
        }

        if(isGet) {
            CommonActivityMgr.addCommonActivityScore(player, goodsId, goodsCount.intValue(), oldValue, nowValue, master, son);
        }else {
            CommonActivityMgr.removeCommonActivityScore(player, goodsId, goodsCount.intValue(), oldValue, nowValue, master, son);
        }
    }

    private long addGoods(BagModule bagModule, int goodsId, BigInteger goodsCount, eLogMoneyType master, eLogMoneyType son) {
        if(goodsCount.compareTo(BigInteger.ZERO) <= 0){
            log.warn("addGoods count:{}, goodsId:{},userId:{},master:{},son:{}", goodsCount.toString(), goodsId, player.getUserId(), master.getValue(), son.getValue());
            return goodsCount.longValue();
        }
        GoodsInfo goodInfo = GoodsMgr.getGoodsById(goodsId);
        if (goodInfo == null) {
            log.error("addGoods goods no fount, goodsId:{},count:{},userId:{},master:{},son:{}", goodsId, goodsCount.toString(), player.getUserId(), master.getValue(), son.getValue());
            return goodsCount.longValue();
        }
        long oldValue = bagModule.getUserBagItemCount(goodsId);
        long nowValue;
        long result = bagModule.addBagItem(goodsId, goodsCount.longValue(), son);
        nowValue = bagModule.getUserBagItemCount(goodsId);
        LogGoodsChange(goodsId, true, goodsCount, oldValue, nowValue, master, son);
        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.OUTPUT,eItemChangeEnums.TypeItem.PROPS,son,goodsId,goodsCount);

        //埋点
        //ResourceChangeLog.trackResourceGetSingle(player, goodInfo, goodsCount, son, oldValue, nowValue);
        return result;
    }

    private boolean removeGoods(BagModule bagModule, int goodsId, BigInteger goodsCount, eLogMoneyType master, eLogMoneyType son) {
        long oldValue = bagModule.getUserBagItemCount(goodsId);
        long nowValue;
        if (goodsCount.longValue() != bagModule.removeBagItem(goodsId, goodsCount.longValue(), son)) {
            return false;
        }
        nowValue = bagModule.getUserBagItemCount(goodsId);
        LogGoodsChange(goodsId, false, goodsCount, oldValue, nowValue, master, son);
        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.CONSUME,eItemChangeEnums.TypeItem.PROPS,son,goodsId,goodsCount);

        //背包埋点
        //ResourceChangeLog.trackResourceUseSingle(player, goodsId, son, goodsCount, oldValue, nowValue);
        return true;
    }

    /**
     * 扣除道具
     * @return
     */
    private boolean removeGoodsToNegative(BagModule bagModule, int goodsId, BigInteger goodsCount, eLogMoneyType master, eLogMoneyType son) {
        long oldValue = bagModule.getUserBagItemCount(goodsId);
        long nowValue;
        if (goodsCount.longValue() != bagModule.removeBagItemCanNegative(goodsId, goodsCount.longValue(), son)) {
            return false;
        }
        nowValue = bagModule.getUserBagItemCount(goodsId);
        LogGoodsChange(goodsId, false, goodsCount, oldValue, nowValue, master, son);
        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.CONSUME,eItemChangeEnums.TypeItem.PROPS,son,goodsId,goodsCount);
        return true;
    }


    /**
     * 扣除道具可以直到负数
     * @return
     */
    public boolean forceRemoveCurrencyCanNegative(Property value){
        if (value != null) {
            List<Integer> goodsIdList = new ArrayList<>(value.getGoods().keySet());
            BagModule bagModule = player.getModule(BagModule.class);
            for (int goodsId : goodsIdList) {
                boolean result = true;
                Property property = new Property(goodsId, value.getCountByGoodsId(goodsId));
                if (currencyIsEnough(property)) {
                    result = removeCurrency(property, eLogMoneyType.GM, eLogMoneyType.GmDeductGoods);
                } else {
                    if (GameConfig.GAME_MONEY_SILVER == goodsId) {
                        if (value.getCountByGoodsId(goodsId).compareTo(removeSilverToNegative(value.getCountByGoodsId(goodsId))) != 0) {
                            result = false;
                        }
                    } else if (GameConfig.GAME_MONEY_INGOTS == goodsId) {
                        long ingots=value.getCountByGoodsId(goodsId).longValue();
                        if (ingots != removeIngotsToNegative(ingots, eLogMoneyType.GM, eLogMoneyType.GmDeductGoods, "")) {
                            result = false;
                        }
//                        else{
//                            PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.CONSUME,eItemChangeEnums.TypeItem.RESOURCE,son,goodsId, BigInteger.valueOf(ingots));
//                            if(master==eLogMoneyType.Mall){
//                                PushMgr.buildShopBuy(player,BigInteger.valueOf(ingots),desc);
//                            }
//                        }
                    } else if (GameConfig.GAME_MONEY_PATRONS_EXP == goodsId) {
                        BigInteger patronsExp= value.getCountByGoodsId(goodsId);
                        if (value.getCountByGoodsId(goodsId)
                                .compareTo(removePatronsExpToNegative(value.getCountByGoodsId(goodsId), eLogMoneyType.GmDeductGoods)) != 0) {
                            result = false;
                        }
                    } else if (GameConfig.GAME_MONEY_PRESTIGE == goodsId) {
                        if (value.getCountByGoodsId(goodsId)
                                .compareTo(removePrestigeToNegative(value.getCountByGoodsId(goodsId), eLogMoneyType.GmDeductGoods)) != 0) {
                            result = false;
                        }
                    } else if (GameConfig.BANQUET_INTEGRAL == goodsId) {//宴会积分
                        if (!player.getModule(BanquetModule.class).removeIntegralToNegative(value.getCountByGoodsId(goodsId).longValue(),
                                eLogMoneyType.GM, eLogMoneyType.GmDeductGoods)) {
//                            return false;
                        }
                    } else if (GameConfig.SECRET_VOLUME_INTEGRAL_ID == goodsId) {//财神密卷积分
                        if (!player.getModule(SecretVolumeModule.class).removeIntegralToNegative(value.getCountByGoodsId(goodsId).longValue(),
                                eLogMoneyType.GM, eLogMoneyType.GmDeductGoods)) {
//                            return false;
                        }
                    } else if (GameConfig.GAME_ITEM_UNION_WEALTH == goodsId) {//联盟财富值 不给用这个接口
//                        return false;
                    }  else if (GameConfig.GAME_ITEM_UNION_CONTRIBUTION == goodsId) {//联盟贡献值 不给用这个接口
//                        return false;
                    }

                    else {
                        result = removeGoodsToNegative(bagModule, goodsId, value.getCountByGoodsId(goodsId),eLogMoneyType.GM, eLogMoneyType.GmDeductGoods);
                    }


                }

            }

        }
        return true;
    }

    public boolean removeCurrency(Property value, eLogMoneyType master, eLogMoneyType son) {
        return removeCurrency(value, master, son, "");
    }

    //isItemRank 是不是参加道具冲榜，自动宴会扣除不会触发榜单
    public boolean removeCurrency(Property value, eLogMoneyType master, eLogMoneyType son, String desc) {
        if (value != null) {
            if (!currencyIsEnough(value)) {
                return false;
            }
            List<Integer> goodsIdList = new ArrayList<>(value.getGoods().keySet());
            BagModule bagModule = player.getModule(BagModule.class);
            for (int goodsId : goodsIdList) {
                boolean result = true;
                if (GameConfig.GAME_MONEY_SILVER == goodsId) {
                    if (value.getCountByGoodsId(goodsId).compareTo(removeSilver(value.getCountByGoodsId(goodsId), son)) != 0) {
                        result = false;
                    }
                } else if (GameConfig.GAME_MONEY_INGOTS == goodsId) {
                    long ingots=value.getCountByGoodsId(goodsId).longValue();
                    if (ingots != removeIngots(ingots, master, son, desc)) {
                        result = false;
                    }else{
                        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.CONSUME,eItemChangeEnums.TypeItem.RESOURCE,son,goodsId, BigInteger.valueOf(ingots));
                        if(master==eLogMoneyType.Mall){
                            PushMgr.buildShopBuy(player,BigInteger.valueOf(ingots),desc);
                        }
                    }
                } else if (GameConfig.GAME_MONEY_PATRONS_EXP == goodsId) {
                    BigInteger patronsExp= value.getCountByGoodsId(goodsId);
                    if (value.getCountByGoodsId(goodsId)
                            .compareTo(removePatronsExp(value.getCountByGoodsId(goodsId), son)) != 0) {
                        result = false;
                    }else{
                        PushMgr.buildItemChangeDetail(player, eItemChangeEnums.Type.CONSUME,eItemChangeEnums.TypeItem.RESOURCE,son,goodsId, patronsExp);
                    }
                } else if (GameConfig.GAME_MONEY_PRESTIGE == goodsId) {
                    if (value.getCountByGoodsId(goodsId)
                            .compareTo(removePrestige(value.getCountByGoodsId(goodsId), son)) != 0) {
                        result = false;
                    }
                } else if (GameConfig.GAME_ITEM_UNION_WEALTH == goodsId) {//联盟财富值
                    //临时
                    CrossUnionModule unionModule = player.getModule(CrossUnionModule.class);
                    if(!unionModule.isUnionWealthEnough(value.getCountByGoodsId(goodsId).longValue())){
                        return false;
                    }
                    unionModule.removeUnionWealth(value.getCountByGoodsId(goodsId).longValue(),
                            master, son);
                    return true;
                } else if (GameConfig.GAME_ITEM_UNION_CONTRIBUTION == goodsId) {//联盟贡献
                    //临时
                    CrossUnionModule unionModule = player.getModule(CrossUnionModule.class);
                    if (!unionModule.isContributionEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                    player.getModule(CrossUnionModule.class).removeContribution(value.getCountByGoodsId(goodsId).intValue(),
                            master, son);
                    return true;
                } else if (GameConfig.BANQUET_INTEGRAL == goodsId) {//宴会积分
                    if (!player.getModule(BanquetModule.class).removeIntegral(value.getCountByGoodsId(goodsId).longValue(),
                            master, son)) {
                        return false;
                    }
                } else if (GameConfig.SECRET_VOLUME_INTEGRAL_ID == goodsId) {//财神密卷积分
                    if (!player.getModule(SecretVolumeModule.class).removeIntegral(value.getCountByGoodsId(goodsId).longValue(),
                            master, son)) {
                        return false;
                    }
                }  else if (GameConfig.STQC_EXCHANGE_SCORE_ITEM_ID == goodsId) {//商途启程，启程币ID
                    //增加累计消耗积分
                    player.getModule(BusinessDepartModule.class).addCostNum(goodsId, value.getCountByGoodsId(goodsId).longValue());
                    result = removeGoods(bagModule, goodsId, value.getCountByGoodsId(goodsId), master, son);
                } else {
                    result = removeGoods(bagModule, goodsId, value.getCountByGoodsId(goodsId), master, son);
                }
                if(result){//消耗成功
                    //成就、任务
                    player.notifyListener(eGamePlayerEventType.ConsumeItem.getValue(), new ConsumeItemArgs(goodsId, value.getCountByGoodsId(goodsId)));
                    if(master != eLogMoneyType.Bag || son != eLogMoneyType.BagComposite) {//不是合成的，才算消耗
                        player.notifyListener(eGamePlayerEventType.ConsumeAddSilverItem.getValue(), new ConsumeItemArgs(goodsId, value.getCountByGoodsId(goodsId)));
                    }
                    //自动商会扣除不算进冲榜
                    if (eLogMoneyType.BanquetCrossAutoJoinSet != son && eLogMoneyType.SuiTangAutoSettingIngotsOperate != son) {
                        //道具消耗冲榜
                        player.notifyListener(eGamePlayerEventType.ConsumeItemRank.getValue(), new ConsumeItemArgs(goodsId, value.getCountByGoodsId(goodsId)));
                    }
                }else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 道具是否足够
     * @param value 道具
     * @return 如果足够返回 true
     */
    public boolean currencyIsEnough(Property value) {
        BagModule bagModule = player.getModule(BagModule.class);

        if (value != null) {
            List<Integer> goodsIdList = new ArrayList<>(value.getGoods().keySet());
            for (int goodsId : goodsIdList) {
                //需求小于0 返回false
                if (value.getCountByGoodsId(goodsId).compareTo(BigInteger.ZERO) < 0) {

                    log.error("currencyIsEnough error,userId:{}, goodsId:{}, count:{}", player.getUserId(), goodsId, value.getCountByGoodsId(goodsId));

                    try {
                        Throwable throwable = new Throwable();
                        log.error("currencyIsEnough error :{} ", throwable);
                    } catch (Exception e) {

                    }
                    return false;
                }

                //如果需求数量等于0 则不需要判断 理论上没有问题
                if (value.getCountByGoodsId(goodsId).compareTo(BigInteger.ZERO) == 0) {
                    continue;
                }
                if (GameConfig.GAME_MONEY_SILVER == goodsId) {
                    if (!isSilverEnough(value.getCountByGoodsId(goodsId))) {
                        return false;
                    }
                } else if (GameConfig.GAME_MONEY_INGOTS == goodsId) {
                    if (!isIngotsEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                } else if (GameConfig.GAME_MONEY_PATRONS_EXP == goodsId) {
                    if (!isPatronsExpEnough(value.getCountByGoodsId(goodsId))) {
                        return false;
                    }
                }  else if (GameConfig.GAME_MONEY_PRESTIGE == goodsId) {
                    if (!isPrestigeEnough(value.getCountByGoodsId(goodsId))) {
                        return false;
                    }
                }  else if (GameConfig.GAME_ITEM_UNION_CONTRIBUTION == goodsId) {//联盟贡献值
                    if (!player.getModule(CrossUnionModule.class).isContributionEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                }  else if (GameConfig.GAME_ITEM_UNION_WEALTH == goodsId) {//联盟财富
                    if (!player.getModule(CrossUnionModule.class).isUnionWealthEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                }else if (GameConfig.BANQUET_INTEGRAL == goodsId) {//宴会积分
                    if (!player.getModule(BanquetModule.class).isIntegralEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                }else if (GameConfig.SECRET_VOLUME_INTEGRAL_ID == goodsId) {//密卷积分
                    if (!player.getModule(SecretVolumeModule.class).isIntegralEnough(value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                } else {
                    if (!bagModule.hasEnoughGoods(goodsId, value.getCountByGoodsId(goodsId).longValue())) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    private boolean isSilverEnough(BigInteger value) {
        BigInteger silver = player.getModule(PlayerModule.class).getUserInfo().getSilver();
        BigInteger preReduceValue = player.getModule(PreReduceModule.class).getPreReduceItemMap().get(GameConfig.GAME_MONEY_SILVER);
        if (null == preReduceValue) {
            preReduceValue = BigInteger.ZERO;
        }
        return silver.add(preReduceValue).compareTo(value) >= 0;
    }

    private boolean isIngotsEnough(long value) {
        BigInteger preReduceValue = player.getModule(PreReduceModule.class).getPreReduceItemMap().get(GameConfig.GAME_MONEY_INGOTS);
        if (null == preReduceValue) {
            preReduceValue = BigInteger.ZERO;
        }
        long temp = preReduceValue.longValue();
        return player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots) + temp >= value;
    }

    private boolean isPatronsExpEnough(BigInteger value) {
        BigInteger exp = player.getModule(PlayerModule.class).getUserInfo().getPatronsExp();
        BigInteger preReduceValue = player.getModule(PreReduceModule.class).getPreReduceItemMap().get(GameConfig.GAME_MONEY_PATRONS_EXP);
        if (null == preReduceValue) {
            preReduceValue = BigInteger.ZERO;
        }
        return exp.add(preReduceValue).compareTo(value) >= 0;
    }

    private boolean isPrestigeEnough(BigInteger value) {
        BigInteger prestige = player.getModule(PlayerModule.class).getUserInfo().getPrestige();
        BigInteger preReduceValue = player.getModule(PreReduceModule.class).getPreReduceItemMap().get(GameConfig.GAME_MONEY_PRESTIGE);
        if (null == preReduceValue) {
            preReduceValue = BigInteger.ZERO;
        }
        return prestige.add(preReduceValue).compareTo(value) >= 0;
    }

    private boolean isExpressionPackage(int goodsId) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (goodsInfo == null) {
            return false;
        }
        return goodsInfo.getType() == eGoodsType.ExpressionPackage.getValue();
    }

    private boolean isTimeLimitHead(int goodsId) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (goodsInfo == null) {
            return false;
        }
        return goodsInfo.getType() == eGoodsType.TimeLimitHead.getValue();
    }

    private void autoUseExpressionPackage(int goodsId, eLogMoneyType master, eLogMoneyType son) {
        OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getAllGoods(goodsId);
        if (openGoodsBagResult == null) {
            return;
        }
        addCurrency(openGoodsBagResult.getReward(), master, son);
    }

    /**
     * 获取能扣除的最大的道具数量，仅元宝和背包道具
     */
    public Property getGoodsCanDeductMax(BagModule bagModule, Property property) {
        Map<Integer, BigInteger> goods = property.getGoods();
        for (int goodsId : goods.keySet()) {
            long oldValue;
            long nowValue;
            if (goodsId == GameConfig.GAME_MONEY_INGOTS) { // 元宝
                oldValue = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            } else { // 其它道具
                oldValue = bagModule.getUserBagItemCount(goodsId);
            }
            nowValue = property.getCountByGoodsId(goodsId).longValue();
            goods.put(goodsId, BigInteger.valueOf(Math.min(oldValue, nowValue)));
        }

        return property;
    }
}
